package cn.edu.njuit.springbootfilmreview.controller;

import cn.edu.njuit.springbootfilmreview.dto.CommentInfo;
import cn.edu.njuit.springbootfilmreview.dto.WantInfo;
import cn.edu.njuit.springbootfilmreview.service.WantInfoService;
import cn.edu.njuit.springbootfilmreview.utils.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Optional;

/**
 * @Author guzy
 * @Description 收藏接口
 * @Date 2022/6/5 10:03
 **/

@RestController
@RequestMapping("/api/want-info")
@Api(value = "收藏接口", tags = "收藏接口")
public class WantInfoController {
    @Autowired
    WantInfoService wantInfoService;


    @ApiImplicitParams({
            @ApiImplicitParam(value = "token", name = "token", dataType = "String", required = true),
            @ApiImplicitParam(value = "用户id", name = "userId", dataType = "Long", required = true),
            @ApiImplicitParam(value = "电影id", name = "movieId", dataType = "Long", required = true)})
    @ApiOperation(value = "添加收藏")
    @PostMapping("add")
    public Result addWantInfo(@RequestParam Long userId,
                              @RequestParam Long movieId,
                              @RequestParam String token) {
        WantInfo wantInfo = new WantInfo();
        wantInfo.setUserId(userId);
        wantInfo.setTime(new Date());
        wantInfo.setMovieId(movieId);
        boolean b = wantInfoService.addWantInfo(wantInfo);
        if (b) {
            return Result.success("收藏添加成功");
        } else {
            return Result.failure("收藏添加失败");
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(value = "token", name = "token", dataType = "String", required = true),
            @ApiImplicitParam(value = "收藏id", name = "id", dataType = "Long", required = true)
    })
    @ApiOperation(value = "删除收藏")
    @DeleteMapping("delete/{id}")
    public Result deleteWantInfoById(@PathVariable Long id, @RequestParam String token) {
        boolean b = wantInfoService.deleteWantInfoById(id);
        if (b) {
            return Result.success("收藏删除成功");
        } else {
            return Result.failure("收藏删除失败");
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(value = "token", name = "token", dataType = "String", required = true),
            @ApiImplicitParam(value = "收藏id", name = "wantInfoId", dataType = "int", required = true)
    })
    @ApiOperation(value = "根据收藏id查找")
    @GetMapping("getAllByWantInfoId/{wantInfoId}")
    public Result getWantInfoById(
            @PathVariable Long wantInfoId,
            @RequestParam String token) {
        Optional<WantInfo> wantInfo = wantInfoService.getWantInfoById(wantInfoId);
        if (wantInfo.isPresent()) {
            return Result.success(wantInfo);
        } else {
            return Result.failure("没有找到收藏");
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(value = "token", name = "token", dataType = "String", required = true),
            @ApiImplicitParam(value = "第几页", name = "currentPage", dataType = "int", required = true),
            @ApiImplicitParam(value = "当前页多少条数据", name = "pageSize", dataType = "int", required = true),
            @ApiImplicitParam(value = "用户id", name = "userId", dataType = "int")
    })
    @ApiOperation(value = "根据用户id查询收藏")
    @GetMapping("getAllByuserId/{currentPage}/{pageSize}")
    public Result getWantInfoListByUserId(
            @PathVariable Integer currentPage,
            @PathVariable int pageSize,
            @RequestParam Long userId,
            @RequestParam String token) {
        IPage<WantInfo> wantInfoIPage = wantInfoService.getWantInfoListByUserId(currentPage, pageSize, userId);
        if (wantInfoIPage.getRecords().size() > 0) {
            return Result.success(wantInfoIPage);
        } else {
            return Result.failure("没有找到收藏");
        }
    }
}
